#include "FileReader.h"
#include <QFile>
#include <QDebug>

FileReader::FileReader(const QString &filePath) : filePath_(filePath) {}

bool FileReader::readBytes(int start, int end) {
    QFile file(filePath_);
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "Failed to open file:" << file.errorString();
        return false;
    }

    if (end < start || start < 0) {
        qDebug() << "Invalid range";
        file.close();
        return false;
    }

    qint64 fileSize = file.size();
    if (end >= fileSize) {
        end = fileSize - 1;
    }

    file.seek(start);
    QByteArray data;
    data.resize(end - start + 1);
    if (file.read(data.data(), data.size()) != data.size()) {
        qDebug() << "Failed to read data from file.";
        file.close();
        return false;
    }

    printData(data);
    file.close();
    return true;
}

void FileReader::printData(const QByteArray &data) {
    qDebug() << "Read data:";
    for (char byte : data) {
        qDebug().nospace() << static_cast<int>(static_cast<unsigned char>(byte));
    }
    qDebug() << "";
}
